#include "Factory.h"

Factory* Factory::sThis = nullptr;

Factory* Factory::getInstance() {
	if (!sThis) {
		sThis = new Factory();
	}
	return sThis;
}

void Factory::registerClass(const char *className, CREATER creator) {
	for (auto it = classPool.begin(); it != classPool.end(); it++) {
		if (strcmp(it->first, className) == 0) {
			printf("%s is already regitered\n", className);
			return;
		}
	}
	printf("%s is regitered\n", className);
	classPool.emplace_back(std::make_pair(className, creator));
}

Base* Factory::create(const char* className) {
	for (auto it = classPool.begin(); it != classPool.end(); it++) {
		if (strcmp(it->first, className) == 0) {
			CREATER creater = it->second;
			return (*creater)();
		}
	}
	printf("%s has not been registered\n", className);
	return nullptr;
}